package com.njtoyo.taxi.admin.service.business.operation_rule.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.lang.Dict;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.njtoyo.netrides.entity.CustomerRegionServiceArrangement;
import com.njtoyo.taxi.admin.library.common.OperationRecordHelper;
import com.njtoyo.taxi.admin.mapper.master.entity.RealtimeCallRule;
import com.njtoyo.taxi.admin.mapper.master.mapper.CustomerRegionServiceArrangementMapper;
import com.njtoyo.taxi.admin.mapper.master.mapper.RealtimeCallRuleMapper;
import com.njtoyo.taxi.admin.rest.presenter.business.operation_rule.RealTimeCallRulePresenter;
import com.njtoyo.taxi.admin.rest.presenter.business.operation_rule.SimpleCallInfoServiceFeeRulePresenter;
import com.njtoyo.taxi.admin.rest.wrapper.business.operation_rule.RealTimeCallRuleUpdateWrapper;
import com.njtoyo.taxi.admin.rest.wrapper.business.operation_rule.RealTimeCallRuleWrapper;
import com.njtoyo.taxi.admin.rest.wrapper.open.on_call.CreateWrapper;
import com.njtoyo.taxi.admin.service.business.CustomerRegionServiceArrangementService;
import com.njtoyo.taxi.admin.service.business.operation_rule.RealtimeCallRuleService;
import com.njtoyo.taxi.entity.backend.AdminUser;
import com.taxi.entity.common.RestResult;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Slf4j
@Service
@RequiredArgsConstructor
public class RealtimeCallRuleServiceImpl implements RealtimeCallRuleService {

    @Autowired
    private CustomerRegionServiceArrangementMapper customerRegionServiceArrangementMapper;
    @Autowired
    private OperationRecordHelper operationRecordHelper;

//    @Override
//    public RestResult getDetail() {
//        return RestResult.success(baseMapper.selectList(null));
//    }
//
//    @Override
//    public RestResult update(RealTimeCallRulePresenter realTimeCallRulePresenter) {
//        List<RealtimeCallRule> realtimeCallRules = baseMapper.selectList(null);
//        if (CollUtil.isEmpty(realtimeCallRules) || realtimeCallRules.size() > 1) {
//            return RestResult.failed("操作失败,数据异常");
//        }
//        RealtimeCallRule realtimeCallRule = realtimeCallRules.get(0);
//        realtimeCallRule.setSendCallRule(JSON.toJSONString(realTimeCallRulePresenter));
//        baseMapper.updateById(realtimeCallRule);
//        //TODO 清除缓存
//        return RestResult.success();
//    }

    @Override
    public IPage<RealTimeCallRuleWrapper> getPage(Integer current, Integer size, String regionName) {
        Page<RealTimeCallRuleWrapper> page = new Page<>(current, size);
        int category = 2;
        IPage<RealTimeCallRuleWrapper> pages = customerRegionServiceArrangementMapper.getPage(page, regionName, category);
        return pages;
    }

    @Override
    public RestResult getDetail(Long id) {
        CustomerRegionServiceArrangement customerRegionServiceArrangement = customerRegionServiceArrangementMapper.selectById(id);
        return RestResult.success(customerRegionServiceArrangement);
    }

    @Override
    public RestResult updateById(AdminUser adminUser, RealTimeCallRuleUpdateWrapper wrapper) {
        //TODO
        CustomerRegionServiceArrangement customerRegionServiceArrangement = customerRegionServiceArrangementMapper.selectById(wrapper.getId());
        JSONObject object = JSONUtil.parseObj(customerRegionServiceArrangement.getDetailSpecification());
        JSONObject sendCallRule = object.getJSONObject("sendCallRule");
        sendCallRule.set("callGapDistance", wrapper.getCallGapDistance());
        sendCallRule.set("callGapTimes", wrapper.getCallGapTimes());
        sendCallRule.set("callGapSecond", wrapper.getCallGapSecond());
        customerRegionServiceArrangement.setDetailSpecification(object.toString());
        customerRegionServiceArrangementMapper.updateById(customerRegionServiceArrangement);
        //TODO 清缓存
        operationRecordHelper.addBusinessLog(String.format("用户[%s] 的修改了发单配置 ", adminUser.getName()));
        return RestResult.success();
    }
}
